'use strict';

const Controller = require('egg').Controller;
const garvinLog = require('../extend/index');
class HomeController extends Controller {
  async index() {
    const { ctx, app } = this;
    const username = ctx.session.username;
    const res = await ctx.service.garvin.getGirl(111);
    const newRes = {
      ...res,
      username,
      skills: [
        '泰式按摩',
        '精油搓背',
        '水疗SPA',
      ],
      currTime: app.currProps
    };
    await ctx.render('index.html', newRes);
    console.log(111111111111, newRes);
    garvinLog(ctx.session.counter);
  }

  async garvin() {
    const { ctx } = this;
    ctx.body = {
      name: 'garvin',
    };
  }

  async add() {
    const ctx = this.ctx;
    ctx.cookies.set('user', 'cookieeeeeeeeee', {
      maxAge: 1000 * 604800,
      httpOnly: false,
      encrypt: true,
    });

    ctx.session.username = 'sessioneeeeeeeeeeeeeee';
    ctx.body = {
      status: 200,
      data: 'Cookie添加成功',
    };
  }

  async del() {
    const ctx = this.ctx;
    ctx.cookies.set('user', undefined);

    ctx.session.username = null;
    ctx.body = {
      status: 200,
      data: 'Cookie删除成功',
    };
  }

  async editor() {
    const ctx = this.ctx;
    ctx.cookies.set('user', 'lllllllllllllllllllll');
    ctx.body = {
      status: 200,
      data: '修改成功',
    };
  }

  async show() {
    const ctx = this.ctx;
    const cookies = ctx.cookies.get('user', { encrypt: true });
    ctx.body = {
      status: 200,
      cookies,
    };
  }

  /**
   * extend上下文扩展ctx
   * 2021-12-14
   */
  async getContext() {
    const ctx = this.ctx;
    const param = ctx.params();
    ctx.body = JSON.stringify(param);
  }

  /**
   * extend上下文扩展request
   * 2021-12-14
   */
  async postRequest() {
    const ctx = this.ctx;
    const token = ctx.request.token;
    ctx.body = {
      status: 200,
      token
    };
  }

  /**
   * extend上下文扩展response
   * 2021-12-14
   */
  async getResponse() {
    const { ctx } = this;
    ctx.response.setToken = '$csacacascscsacaca';

    const testBase64 = ctx.helper.base64Encode('garvin>>>>>>>>>>');
    ctx.body = testBase64;
  }
}


module.exports = HomeController;
